//
//  RXXKVideoControlView.m
//  RXXKVideo
//
//  Created by 星仔 on 2019/3/17.
//  Copyright © 2019年 rxx. All rights reserved.
//

#import "const.h"
#import "RXXKVideoModel.h"
#import "RXXKVideoControlView.h"
#import "GKSliderView.h"

@interface RXXVideoItemButton : UIButton

@end
@implementation RXXVideoItemButton

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.imageView sizeToFit];
    [self.titleLabel sizeToFit];
    CGFloat w = self.frame.size.width;
    CGFloat h = self.frame.size.height;
    
    CGFloat imageW = self.imageView.frame.size.width;
    CGFloat imageH = self.imageView.frame.size.height;
    
    self.imageView.frame = CGRectMake((w-imageH)/2.0, 0, imageW, imageH);
    CGFloat titleW = self.titleLabel.frame.size.width;
    CGFloat titleH = self.titleLabel.frame.size.height;
    self.titleLabel.frame = CGRectMake((w-titleW)/2.0, h-titleH, titleW, titleH);
}

@end

@interface RXXKVideoControlView()

@property (nonatomic, strong) UIImageView             *iconImageView;
@property (nonatomic, strong) RXXVideoItemButton      *praiseBtn;
@property (nonatomic, strong) RXXVideoItemButton      *commitBtn;
@property (nonatomic, strong) RXXVideoItemButton      *shareBtn;

@property (nonatomic, strong) UILabel                 *nameLabel;
@property (nonatomic, strong) UILabel                 *contentLabel;

@property (nonatomic, strong) UIActivityIndicatorView      *loadingView;
@property (nonatomic, strong) GKSliderView                 *sliderView;
@property (nonatomic, strong) UIButton                     *playBtn;

@end
@implementation RXXKVideoControlView
#pragma mark - ♻️life cycle
- (instancetype)init{
    if (self = [super init]) {
        [self addSubview:self.coverImageView];
        [self addSubview:self.iconImageView];
        [self addSubview:self.praiseBtn];
        [self addSubview:self.commitBtn];
        [self addSubview:self.shareBtn];
        
        [self addSubview:self.nameLabel];
        [self addSubview:self.contentLabel];
        [self addSubview:self.loadingView];
        [self addSubview:self.sliderView];
        [self addSubview:self.playBtn];
        
        [self.coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        CGFloat bottomH = RX_TABBAR_HEIGHT;
        [self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.bottom.equalTo(@(-bottomH));
            make.height.equalTo(@(1.0 * ScreenScale));
        }];
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(30 * ScreenScale);
            make.width.equalTo(@(504*ScreenScale));
            make.bottom.equalTo(self.sliderView.mas_top).offset(-30*ScreenScale);
        }];
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(30*ScreenScale);
            make.bottom.equalTo(self.contentLabel.mas_top).offset(-20*ScreenScale);
        }];
        [self.shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).offset(-40*ScreenScale);
            make.bottom.equalTo(self.nameLabel.mas_top).offset(-50*ScreenScale);
            make.height.equalTo(@(110*ScreenScale));
        }];
        [self.commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.shareBtn.mas_centerX);
            make.bottom.equalTo(self.shareBtn.mas_top).offset(-45*ScreenScale);
            make.height.equalTo(@(110*ScreenScale));
        }];
        [self.praiseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.shareBtn.mas_centerX);
            make.bottom.equalTo(self.commitBtn.mas_top).offset(-45*ScreenScale);
            make.height.equalTo(@(110*ScreenScale));
        }];
        [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.shareBtn.mas_centerX);
            make.bottom.equalTo(self.praiseBtn.mas_top).offset(-70*ScreenScale);
            make.width.height.equalTo(@(100*ScreenScale));
        }];
        [self.loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
        [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentViewClick:)];
        [self addGestureRecognizer:tap];
    }
    return self;
}
#pragma mark - 🔒private
- (void)setProgress:(CGFloat)progress{
    self.sliderView.value = progress;
}
- (void)startLoading{
    [self.loadingView startAnimating];
}
- (void)stopLoading{
    [self.loadingView stopAnimating];
}
- (void)showPlayBtn{
    self.playBtn.hidden = NO;
}
- (void)hidePlayBtn{
    self.playBtn.hidden = YES;
}
#pragma mark - 🚪public
- (void)setVideoModel:(RXXKVideoModel *)videoModel{
    _videoModel = videoModel;
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:_videoModel.thumbnail_url] placeholderImage:[UIImage imageNamed:@"placeholderimg"]];
    self.nameLabel.text = videoModel.author.name_show;
    self.contentLabel.text = videoModel.title;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:_videoModel.author.portrait] placeholderImage:[UIImage imageNamed:@"placeholderimg"]];
    [self.praiseBtn setTitle:_videoModel.agree_num forState:UIControlStateNormal];
    [self.commitBtn setTitle:_videoModel.comment_num forState:UIControlStateNormal];
    [self.shareBtn setTitle:_videoModel.share_num forState:UIControlStateNormal];
}
#pragma mark - 🍐delegate
#pragma mark - 🔄overwrite
#pragma mark - ☎️notification
#pragma mark - 🎬event response
- (void)contentViewClick:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(contentViewDidClickSelf:)]) {
        [self.delegate contentViewDidClickSelf:self];
    }
}
- (void )praiseBtnClick:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(contentViewDidClickPraise:)]) {
        [self.delegate contentViewDidClickPraise:self];
    }
}
- (void)iconClick:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(contentViewDidClickIcon:)]) {
        [self.delegate contentViewDidClickIcon:self];
    }
}
- (void)commitBtnClick:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(contentViewDidClickCommit:)]) {
        [self.delegate contentViewDidClickCommit:self];
    }
}
- (void)shareBtnClick:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(contentViewDidClickShare:)]) {
        [self.delegate contentViewDidClickShare:self];
    }
}
#pragma mark - ☸getter and setter
- (UIImageView *)coverImageView{
    if (!_coverImageView) {
        _coverImageView = [UIImageView  new];
        _coverImageView.clipsToBounds = YES;
    }
    return _coverImageView;
}
- (UIImageView *)iconImageView{
    if (!_iconImageView) {
        _iconImageView = [UIImageView new];
        _iconImageView.layer.cornerRadius = 50*ScreenScale;
        _iconImageView.layer.masksToBounds = YES;
        _iconImageView.layer.borderWidth = 1.0;
        _iconImageView.layer.borderColor = [UIColor whiteColor].CGColor;
        _iconImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconClick:)];
        [_iconImageView addGestureRecognizer:tap];
    }
    return _iconImageView;
}
- (RXXVideoItemButton *)praiseBtn{
    if (!_praiseBtn) {
        _praiseBtn = [RXXVideoItemButton new];
        [_praiseBtn setImage:[UIImage imageNamed:@"ss_icon_star_normal"] forState:UIControlStateNormal];
        [_praiseBtn setImage:[UIImage imageNamed:@"ss_icon_star_selected"] forState:UIControlStateSelected];
        [_praiseBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_praiseBtn.titleLabel setFont:[UIFont systemFontOfSize:13.0]];
        [_praiseBtn addTarget:self action:@selector(praiseBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _praiseBtn;
}
- (RXXVideoItemButton *)commitBtn{
    if (!_commitBtn) {
        _commitBtn = [RXXVideoItemButton new];
        [_commitBtn setImage:[UIImage imageNamed:@"ss_icon_comment"] forState:UIControlStateNormal];
        [_commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_commitBtn.titleLabel setFont:[UIFont systemFontOfSize:13.0]];
        [_commitBtn addTarget:self action:@selector(commitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _commitBtn;
}
- (RXXVideoItemButton *)shareBtn{
    if (!_shareBtn) {
        _shareBtn = [RXXVideoItemButton new];
        [_shareBtn setImage:[UIImage imageNamed:@"ss_icon_share"] forState:UIControlStateNormal];
        [_shareBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_shareBtn.titleLabel setFont:[UIFont systemFontOfSize:13.0]];
        [_shareBtn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _shareBtn;
}
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = [UIFont systemFontOfSize:15.0];
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconClick:)];
        [_nameLabel addGestureRecognizer:tap];
    }
    return _nameLabel;
}
- (UILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [UILabel new];
        _contentLabel.font = [UIFont systemFontOfSize:13.0];
        _contentLabel.textColor = [UIColor whiteColor];
    }
    return _contentLabel;
}
- (UIActivityIndicatorView *)loadingView{
    if (!_loadingView) {
        _loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        _loadingView.hidesWhenStopped = YES;
    }
    return _loadingView;
}
- (GKSliderView *)sliderView{
    if (!_sliderView) {
        
        _sliderView = [GKSliderView new];
        _sliderView.isHideSliderBlock = YES; //如果为yes时滑块不能点击
        _sliderView.sliderHeight = ScreenScale *1.0;
        _sliderView.maximumTrackTintColor = [UIColor grayColor];
        _sliderView.minimumTrackTintColor = [UIColor whiteColor];
    }
    return _sliderView;
}
- (UIButton *)playBtn{
    if (!_playBtn) {
        _playBtn = [UIButton new];
        [_playBtn setImage:[UIImage imageNamed:@"ss_icon_pause"] forState:UIControlStateNormal];
        _playBtn.hidden = YES;
    }
    return _playBtn;
}
@end
